package concurrencyPractice.chapter04;

import concurrencyPractice.GuardBy;
import concurrencyPractice.ThreadSafe;

import java.util.HashSet;
import java.util.Set;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 程序4-2 通过封闭机制确保线程安全
 * @date 2023/12/28 14:42:01
 */
@ThreadSafe
public class PersonSet<Person> {

    @GuardBy("this")
    private final Set<Person> mySet = new HashSet<>();

    public synchronized void addPerson(Person p) {
        mySet.add(p);
    }

    public synchronized boolean containPerson(Person p) {
        return mySet.contains(p);
    }
}
